
var post_login = function(type)
{
	$("#sign_up_error").hide();
	var user = $("#email").val();
	var psw = $("#password").val();
	var captcha = $("#captcha_text").val();
	var remember = $("#remember").is(":checked") ? 1 : 0;
	if((user == "" || psw == "") && type != "cookie")
	{
		$("#sign_up_error").html(pagecontent[16]).show();
		return false;
	}
	button_disable($("#login_button"));
	$("#login_button").after($(get_loading_img()).attr("id","login_loading"));
	$.post("/ajax/login.ajax.php",{"type":type,"user":user,"password":psw,"remember":remember,"captcha":captcha},function(data){
		if(data != null)
		{
			if(data["success"])
			{
				$("#login_loading").remove();
				$("#sign_up_error").hide();
				$("#login_form").attr("success",1);
				$("#login_form").submit();
			}
			else
			{ // error
				if (data["error"]=="Banned")
				{ // banned
					window.location="/banned/"+data["ban_info"]["md_club"]+"/";
				}
				else if (data["error"]=="no_club")
				{
					window.location="/banned/no_club/"+data["user"]+"/";
				}
				else
				{ // error
					$("#login_loading").remove();
					if(data["captcha"])
					{
						if($("#captcha_image").length > 0)
						{
							if($("#captcha_image").is(":hidden")) modal.show();
							$("#captcha_prompt").find(".error").remove();
							$("#captcha_text").closest(".emboss_outer").before($("<p/>").addClass("error").text(data["error"]));
							$("#captcha_text").val("");
							change_captcha();
						}
						else
						{
							var cap_html = $("<div />").attr("id","captcha_prompt").append(
								$("<h2 />").html("Security"),
								$("<p />").html("Write the letters from the image below."),
								$("<img />").attr("id","captcha_image"),
								$("<span />").addClass("captcha_refresh").click(function(){change_captcha()}), // class="captcha_refresh" onclick="change_captcha()" title="Change Letters"></span>
								$("<div />").html($("<input />").attr("type","text").addClass("emboss").attr("id","captcha_text").keypress(function(e){if(e.keyCode==13) {post_login(); return false;}})),
								$("<div/>").addClass("msgbuttons").html(
									$(make_button("Log in","post_login()")),
									$(make_button(global_content["cancel"],"modal.hide()"))
								)
							);
							modal.html(cap_html).show();
							emboss();
							change_captcha();
						}
					}
					else
					{
						$("#sign_up_error").html(data["error"]).show();
					}
					button_enable($("#login_button"));
				}
			}
		}
	},"json")
	.error(function(){
		setTimeout(function(){
		button_enable($("#login_button"));
		$("#login_loading").remove();
		$("#sign_up_error").html(global_content[394]).show();
		},500);
	});
}
function ajax_submit(id,post_function)
{
	if($(id).attr("success") != "1") setTimeout(post_function,10);
	return $(id).attr("success") == "1";
}
function show_language_select() {
	$("#show_language_select_link").hide(200);
	$("#language_select").show(200);
}

function change_language(lang){
	lang = lang || $("#language_select").val();
	var loc = "/"+lang+"/";
	if (onfb=='true') loc = loc + "?fb";
	window.location=loc;
}
$(function(){
	$("#email").focus();
	$("#country").selectmenu({
		"style":"dropdown",
		"maxHeight":"400",
		"menuWidth":"200",
		"width": "200",
		format: flag_format
	});

	$(".screenshot_frame").click(
		function() {
			pop_screenshot(this.href);
			return false;
		}
	);

	make_checkbox("remember");

	/* Email */
	$("#signup_email").keyup(function() {
		validate_email(true);
	}).change(function() {
		validate_email();
	});

	/* Password */
	$("#signup_password").keyup(function() {
		validate_password(true);
	}).change(function() {
		validate_password();
	});

	/* Re-password */
	$("#signup_password_re").keyup(function() {
		validate_repassword(true);
	}).change(function() {
		validate_repassword();
	});

	preload("/pics/errow.png");
	preload("/pics/circle_check.png");

	$("#login_button").keypress(function(e){
		if(e.keyCode==13)
		{
			$(this).click();
			return false;
		}
	});
	if(hide_login)
	{
		$("#login_form").hide().after($("<span />").addClass("faux_link").html(pagecontent[33]).click(function(){
			$("#login_form").show();
			$(this).remove();
		}));
	}
});

function submit_login() {
	$("#login_form")[0].submit();
}

function pop_screenshot(url) {

	$("body").append(
		$("<img id='ss_img' src='"+url+"'/>").hide()
	)

	modal.html(get_loading_img());
	modal.outer().css({"width":"auto","cursor":"pointer"}).click(function() {modal.hide()});
	modal.show();

	$("#ss_img").load(function() {
		$("#ss_img").remove();
		modal.transition("<img id='ss_img' src='"+url+"'/>");
	});

}

function make_errow(text, where) {
	text = text || "Error";
	if (text == "clear") {
		$(where).find(".sign_up_error").fadeOut(500, function() { $(this).remove(); });
	} else {
		if ($(where).find(".sign_up_error").length <= 0) {
			$("<div class='sign_up_error'>").prependTo(where).html(text).fadeIn(500)
		}
	}
}

function validate_email(skip_error) {
	var reg_exp = /\b[\w\.-]+@[\w\.-]+\.\w{2,4}\b/;
	var valid = reg_exp.test($("#signup_email").val());

	if (valid) {
		make_errow("clear", "#email_field");
		$("#signup_email").css("background-image","url('/pics/circle_check.png')");
	} else {
		$("#signup_email").css("background-image","none");
		if (!skip_error) {
			make_errow(pagecontent[18], "#email_field");
		}
	}
	return valid;
}

function validate_password(skip_error) {
	var valid = ($("#signup_password").val().length >= 6);
	if (valid) {
		make_errow("clear", "#password_field");
		$("#signup_password").css("background-image","url('/pics/circle_check.png')");
	} else {
		$("#signup_password").css("background-image","none");
		if (!skip_error) {
			make_errow(pagecontent[17], "#password_field");
		}
	}
	return valid;
}

function validate_repassword(skip_error) {
	var password_valid = ($("#signup_password").val().length >= 6);
	var valid = ($("#signup_password").val() == $("#signup_password_re").val());

	if (!password_valid) {
		make_errow("clear", "#repassword_field");
		$("#signup_password_re").css("background-image","none");
	} else if (valid && password_valid) {
		make_errow("clear", "#repassword_field");
		$("#signup_password_re").css("background-image","url('/pics/circle_check.png')");
	} else {
		$("#signup_password_re").css("background-image","none");
		if (!skip_error) {
			make_errow(pagecontent[19], "#repassword_field");
		}
	}

	return valid;
}

function validate_register() {
	click_action_log(35,referral);
	var email = validate_email();
	var password = validate_password();
	var repassword = validate_repassword();

	if(!email) click_action_log(42,referral);
	if(!password) click_action_log(43,referral);
	if(!repassword) click_action_log(44,referral);

	if (email && password && repassword) {
		click_action_log(36,referral);
		return true;
	}

	return false;
}